package com.cc.user.controller;

import com.cc.common.utils.ResponseData;
import com.cc.user.entity.DemandnoteEntity;
import com.cc.user.entity.UserEntity;
import com.cc.user.service.DemandnoteService;
import com.cc.user.service.EmployeeCustomerService;
import com.cc.user.service.ScheduleService;
import com.cc.user.vo.DueVo;
import com.cc.user.vo.IndividualVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("user/individualcenter")
@Api(tags="个人主页面")
public class IndividualCenterController {

    @Autowired
    private EmployeeCustomerService employeeCustomerService;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private DemandnoteService demandnoteService;


    @GetMapping("/home")
    @ApiOperation("进入个人中心主页面")
    public ResponseData<IndividualVo> home(HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();

        //获取未读待办信息的个数
        int notreadinfo_num = scheduleService.getNotreadInfoNum(uid);

        //获取所属业务员昵称
        UserEntity employee = employeeCustomerService.getEmployee(uid);
        String employee_name = "";
        if(employee != null) {
            employee_name = employee.getNickname();
        }


        //获取用户所有缴款记录
        List<DemandnoteEntity> list1 = demandnoteService.getAllDemandnote(uid);
        //累加缴款金额总和
        BigDecimal receiveMoneySum = new BigDecimal(0);
        for(DemandnoteEntity demandnoteEntity: list1)
            receiveMoneySum.add(demandnoteEntity.getReceiveMoney());

        //获取用户所有线下订单
        List<DueVo> list2 = demandnoteService.getAllYJKById(uid);
        //累加线下订单金额总和
        BigDecimal payMoneySum = new BigDecimal(0);
        for(DueVo dueVo: list2)
            payMoneySum = payMoneySum.add(dueVo.getAmountsPayable());

        IndividualVo individualVo = new IndividualVo();
        individualVo.setEmployee_name(employee_name);
        individualVo.setNotreadinfo_num(notreadinfo_num);
        individualVo.setPayMoneySum(payMoneySum);
        individualVo.setReceiveMoneySum(receiveMoneySum);
        individualVo.setAvatar(user.getAvatar());
        individualVo.setNickname(user.getNickname());

        return ResponseData.success(individualVo);

    }

}
